package com.exsun.ex1077.ui.fragment.vehicletrack

import android.os.Bundle
import com.bigkoo.pickerview.builder.TimePickerBuilder
import com.drake.brv.utils.divider
import com.drake.brv.utils.linear
import com.drake.brv.utils.models
import com.drake.brv.utils.setup
import com.exsun.ex1077.R
import com.exsun.ex1077.databinding.FragmentVehicleTrackBinding
import com.exsun.ex1077.network.entity.MapVehicle
import com.exsun.ex1077.network.entity.VehicleTrack
import com.exsun.ex1077.ui.activity.trackdetail.TrackDetailActivity
import com.zhangping.fastjetpack.base.BaseFragment
import com.zhangping.fastjetpack.extend.*
import com.zhangping.fastjetpack.extend.base.parseState
import kotlinx.android.synthetic.main.fragment_vehicle_track.*
import kotlinx.android.synthetic.main.fragment_vehicle_track.custom
import kotlinx.android.synthetic.main.fragment_vehicle_track.search

/**
 * 描述：车辆轨迹
 * 作者：ZhangPing
 * 创建时间：2022/8/10 10:53
 */
class VehicleTrackFragment: BaseFragment<VehicleTrackViewModel, FragmentVehicleTrackBinding>() {

    override fun layoutId() = R.layout.fragment_vehicle_track

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.vm = mViewModel
        mViewModel.model.set(requireArguments().getSerializable("model") as MapVehicle?)
        super.initView(savedInstanceState)
        trackListView.linear().divider {
            setDivider(1, true)
            setColor(R.color.divider.toColorRes())
        }.setup {
            addType<VehicleTrack>(R.layout.list_item_track)
            onClick(R.id.itemRoot){
                toActivity<TrackDetailActivity>(Bundle().apply {
                    putSerializable("model", getModel<VehicleTrack>())
                    putInt("objType", mViewModel.model.get()?.objType ?: 0)
                })
            }
        }
        today.setOnClickListener {
            if (!mViewModel.customTime.get()) return@setOnClickListener
            bulgeLine.setBulgePosition(35)
            stateLayout.showContent()
            mViewModel.apply {
                customTime.reversal()
                loadData()
            }
        }
        custom.setOnClickListener {
            if (mViewModel.customTime.get()) return@setOnClickListener
            bulgeLine.setBulgePosition(99)
            mViewModel.apply {
                customTime.reversal()
                startTime.set("")
                endTime.set("")
            }
            stateLayout.showContent()
            trackListView.models = emptyList()
        }
        //开始时间选择器
        val startTimePicker = TimePickerBuilder(activity) { date, view ->
            mViewModel.startTime.set(date.toDateTimeStr())
        }.setType(booleanArrayOf(true,true,true,true,true,true)).setTitleText("开始时间").build()
        //结束时间选择器
        val endTimePicker = TimePickerBuilder(activity) { date, view ->
            mViewModel.endTime.set(date.toDateTimeStr())
        }.setType(booleanArrayOf(true,true,true,true,true,true)).setTitleText("结束时间").build()
        startTime.setOnClickListener {
            startTimePicker.show()
        }
        endTime.setOnClickListener {
            endTimePicker.show()
        }
        search.setOnClickListener {
            //执行查询
            mViewModel.apply {
                if (startTime.get().isEmpty()) {
                    "请选择开始时间".showToast()
                    return@setOnClickListener
                }
                if (endTime.get().isEmpty()) {
                    "请选择结束时间".showToast()
                    return@setOnClickListener
                }
                if (startTime.get().afterDate(endTime.get())) {
                    "开始时间不能大于结束时间".showToast()
                    return@setOnClickListener
                }
                mViewModel.loadData()
            }
        }
        stateLayout.showLoading()
        mViewModel.loadData()
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.resultLiveData.observe(this){
            parseState(it, {
                if (it.isNullOrEmpty()){
                    stateLayout.showEmpty()
                }else{
                    stateLayout.showContent()
                    trackListView?.models = it
                }
            }, {
                stateLayout.showError()
            })
        }
    }
}